--[[
	Copyright (C) 2008 miniwrom

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]

Oscillo = LibStub("AceAddon-3.0"):NewAddon("Oscillo", "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")

local HealComm = LibStub:GetLibrary("LibHealComm-3.0");
local Graph = LibStub:GetLibrary("LibGraph-2.0");
local self = Oscillo

function Oscillo:OnInitialize()
	Oscillo:Print("Initialize")
	self.db = LibStub("AceDB-3.0"):New("OscilloDB")
end

function Oscillo:OnEnable()
    --LibHealComm-3.0:register enents
	HealComm.RegisterCallback(self, "HealComm_DirectHealStart");
	HealComm.RegisterCallback(self, "HealComm_DirectHealStop");
	HealComm.RegisterCallback(self, "HealComm_DirectHealDelayed");
	Oscillo:RegisterEvent("PLAYER_TARGET_CHANGED")
	
	--AceConsole-3.0:info
	Oscillo:Print("Enable, type '/osc' for more infomation.")
	
	if of then return end
	of = CreateFrame("Frame", "OscillaFrame", UIParent)
	of:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",})
	of:SetFrameStrata("BACKGROUND")
	of:SetPoint("BOTTOMLEFT", UIParent,"CENTER" , 0, 0)
	of:SetBackdropColor(1,1,1,0.3)
	of:SetWidth(250)
	of:SetHeight(100)
	of:Show()
	of:EnableMouse(true)
	of:RegisterForDrag("LeftButton")
	of:SetMovable(true)
	of:SetScript("OnDragStart", function() this:StartMoving() end)
	of:SetScript("OnDragStop", function() this:StopMovingOrSizing()	end)
end

function Oscillo:OnDisable()
    Oscillo:Print("Disable")
	of:Hide()
end

--[[
Oscillo:RegisterChatCommand("osc", "SlashProcessorFunc")

function Oscillo:SlashProcessorFunc(input)
  -- Process the slash command ('input' contains whatever follows the slash command)
    Oscillo:Print("Slash Info")
end
]]

--[[
local options = {
    name = "Oscillo",
    handler = Oscillo,
    type = 'group',
    args = {
        msg = {
            type = 'input',
            name = 'testname',
            desc = 'this is a test',
            set = 'SetMyMessage',
            get = 'GetMyMessage',
        },
    },
}

function Oscillo:GetMyMessage(info)
    return myMessageVar
end

function Oscillo:SetMyMessage(info, input)
    myMessageVar = input
end

LibStub("AceConfig-3.0"):RegisterOptionsTable("Oscillo", options, {"osc", "oscillo"})
]]

--[[
function Oscillo:MyFunction()
    self.db.char.myVal = "My character-specific saved value"
    self.db.global.myOtherVal = "My global saved value"
end
--self.db:SetProfile("NewActiveProfile")
Oscillo:RegisterEvent("HealComm_DirectHealStart")
]]

function Oscillo:PLAYER_TARGET_CHANGED()
	if not ("target") then return
	end
	
	GetPlayerHealingState(unit)
	DrawOscillo(target)

end

h = {}
incomingHealBefore = {}
incomingHealAfter = {}
nextTime = {}
nextSize = {}
nextName = {}

function GetPlayerHealingState(unit)

	--h = { HealComm:UnitCastingHealGet('player')} 
	
	local a = select(1, HealComm:UnitCastingHealGet("player"))
	
	
	Oscillo:Print(a)
	--Oscillo:Print(unit)
	
--[[
	local a , b , c , d , e = HealComm:UnitIncomingHealGet('player' , GetTime())
	Oscillo:Print(a)
	Oscillo:Print(b)
	Oscillo:Print(c)
	Oscillo:Print(d)
	Oscillo:Print(e)
	]]
	
end

function DrawOscillo(unit)
	--[[if not (unit=="player")then return
	else
		
	]]
	local totaltest  = select(2 , HealComm:UnitCastingHealGet(unit))
	Oscillo:Print(select(2 , HealComm:UnitCastingHealGet(unit)))
	Oscillo:Print(totaltest)
	
	
	
	
	local i= 0
	local t = 0
	
	--循环开始
	
	
	
	local nextsize = select (4, HealComm:UnitIncomingHealGet(unit, GetTime()+i));
	local nexttime = select (3, HealComm:UnitIncomingHealGet(unit, GetTime()+i));
	i = i + nexttime - GetTime();
	Oscillo:Print(select(5 , HealComm:UnitIncomingHealGet(unit, GetTime()+i)));
	
	local bar[t] = CreateFrame("StatusBar", nil, of)
	bar[t]:SetStatusBarTexture("Interface\\Tooltips\\UI-Tooltip-Background")
	bar[t]:ClearAllPoints()
	bar[t]:SetPoint('TOPLEFT',of,'TOPLEFT')
	bar[t]:SetMinMaxValues(0, 1)
	bar[t]:SetMovable(true)
	bar[t]:SetStatusBarColor(0,1,0)
	bar[t]:SetWidth(150)
	bar[t]:SetHeight(nextsize)
	of:SetScript("OnUpdate",
	function() 
		bar[t]:SetValue((nexttime- GetTime())/2.5)
	end)

	
	t= t + 1;
    end
	
	
	
	
	

	
	
--[[

local bar = {}

bar=CreateFrame("StatusBar", nil, of)
bar:SetStatusBarTexture("Interface\\Tooltips\\UI-Tooltip-Background")
bar:ClearAllPoints()
bar:SetPoint('BOTTOMLEFT',of,'CENTER')
bar:SetWidth(150)
bar:SetHeight(10)
bar:SetMinMaxValues(0, 1)
bar:SetMovable(true)
bar:SetValue(0)
bar:SetStatusBarColor(0,1,0)

]]


function Oscillo:HealComm_DirectHealStart(event, healerName, healSize, endTime, ...)
    for i=1,select('#', ...) do
        local targetName = select(i, ...);
			Oscillo:Print("开始" .. healerName .."->".. targetName .."+".. healSize .." @".. endTime-GetTime() .."s ]]");
			--[[bar:SetHeight(healSize/500)
			bar:Show()
of:SetScript("OnUpdate",
	function() 
		bar:SetValue((endTime-GetTime())/2.5)
	end)
			
    end]]
	
	DrawOscillo(healerName)
end






--[[
local g=Graph:CreateGraphRealtime("TestRealtimeGraph",of,"BOTTOMLEFT","CENTER",0,0,300,300)
g:SetAutoScale(false)
g:SetGridSpacing(0.5,20.0)
g:SetYMax(200)
g:SetXAxis(-2.5,0)
g:SetMode("RAW")
g:SetBarColors({0.1,0.4,0.0,1.0},{0.0,0.6,0.0,1.0})
DEFAULT_CHAT_FRAME:AddMessage("绘图完成")
]]

--[[
f = CreateFrame("Frame")
f:SetScript("OnUpdate",
	function() 
			s = Graph:CreateGraphRealtime("healline",f,"CENTER","CENTER",0,0,(endTime-GetTime())*60,healSize*2)
s:SetMode("RAW")
			
	end)
		TestRealtimeGraphRawUpdate(healSize, endTime)
			--s:SetBarColors({0.1,0.4,0.0,1.0},{0.0,0.6,0.0,1.0})
]]		









--[[
local function OnUpdate()
barSpark:ClearAllPoints()
barSpark:

end
]]




--[[

function TestRealtimeGraphRawUpdate(healSize, endTime)

	s = Graph:DrawVLine(of, (endTime-GetTime())*100, 0, healSize*3, 50 , {0.0,0.0,1.0,1.0})
	
	of:SetScript("OnUpdate",function() 
	s:SetVertexColor((endTime-GetTime())*0.2,0.5,1,(endTime-GetTime())*0.2)
	s:SetPoint('CENTER', UIParent, 'LEFT', (endTime-GetTime())*100, 0)
	end)
	--g:DrawVLine(Graph,(endTime-GetTime()),0,healSize,{0.0,0.6,0.0,1.0})
	--g:AddBar((endTime-GetTime()) * 100)
	

end

]]




--[[
local g=Graph:CreateGraphLine("TestLineGraph",UIParent,"CENTER","CENTER",0,0,400,400)
g:SetXAxis(0,2.5)
g:SetYAxis(0,100)
g:SetGridSpacing(0.5,10)
g:SetGridColor({0.5,0.5,0.5,0.5})
g:SetAxisDrawing(false,false)
g:SetAxisColor({1.0,1.0,1.0,1.0})
g:SetAutoScale(false)
--DEFAULT_CHAT_FRAME:AddMessage("坐标轴初始化完成")

function TestRealtimeGraphUpdate(healSize, endTime)

	local Data1={{endTime-GetTime(),0},{endTime-GetTime(),healSize}}
	g:AddDataSeries(Data1,{1.0,0.0,0.0,1.0})
	g:SetScript("OnUpdate", g.SetXAxis(0,1))
	DEFAULT_CHAT_FRAME:AddMessage("绘制曲线完成")

end

]]

   
   
   
   
   
   
   
   
   
   
   
   
	


--[[
function TestRealtimeGraphRawUpdate()
local g=Graph:CreateGraphRealtime("TestRealtimeGraph",UIParent,"CENTER","CENTER",0,0,300,300)
g:SetAutoScale(false)
g:SetGridSpacing(0.5,20.0)
g:SetYMax(200)
g:SetXAxis(-2.5,0)
g:SetMode("RAW")
g:SetBarColors({0.1,0.4,0.0,1.0},{0.0,0.6,0.0,1.0})
DEFAULT_CHAT_FRAME:AddMessage("绘图完成")

	f = CreateFrame("Frame")
	f.frames=0
	f.NextUpdate=GetTime()

	f:SetScript("OnUpdate",function() 
			if f.NextUpdate>GetTime() then
				return
			end
			
			g:AddBar(UnitHealth("player"))
			f.NextUpdate= f.NextUpdate + g.BarWidth
		end)
	f:Show()

end
]]

































function Oscillo:HealComm_DirectHealStop(event, healerName, healSize, succeeded, ...)
	
    for i=1,select('#', ...) do
        local targetName = select(i, ...);
		--if ( succeeded == false ) then
		DEFAULT_CHAT_FRAME:AddMessage("--[[DEBUG 中断" .. healerName .."->".. targetName .." ]]");
		bar:Hide()
		--end
    end
	
end

function Oscillo:HealComm_DirectHealDelayed(event, healerName, healSize, endTime, ...)	
    for i=1,select('#', ...) do
        local targetName = select(i, ...);
		DEFAULT_CHAT_FRAME:AddMessage("--[[DEBUG 延迟" .. healerName .."->".. targetName .."+".. healSize .." @".. endTime-GetTime() .."s ]]");
		
	end	
end